package org.bing.balancer.impl.proxy;

import java.net.InetSocketAddress;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

public class HexDumpProxy {

    public HexDumpProxy(String addr, int port) {

    }

    public void start() {
        String[] args = new String[] { "localhost", "8080", "www.alibaba.com", "80" };
        // Parse command line options.
        String localAddress = args[0];
        int localPort = Integer.parseInt(args[1]);
        String remoteHost = args[2];
        int remotePort = Integer.parseInt(args[3]);

        System.err.println("Proxying " + localAddress + ":" + localPort + " to " + remoteHost + ':' + remotePort + " ...");

        // Configure the bootstrap.
        Executor executor = Executors.newCachedThreadPool();
        ServerBootstrap sb = new ServerBootstrap(new NioServerSocketChannelFactory(executor, executor));

        // Set up the event pipeline factory.
        ClientSocketChannelFactory cf = new NioClientSocketChannelFactory(executor, executor);

        sb.setPipelineFactory(new HexDumpProxyPipelineFactory(cf, remoteHost, remotePort));

        // Start up the server.
        sb.bind(new InetSocketAddress(localAddress, localPort));
    }
}
